package edu.uwlax.cs.oayonlinestore.client.gui.admin;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.gui.Store;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.SwitchPage;
import edu.uwlax.cs.oayonlinestore.client.utils.Title;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.UserDetailsVO;

public class PageListAccounts extends TitlePage {

	public class AccountTable extends DekoTable {
		public AccountTable() {
			super();
			String anchor = Store.getFrame().getAdminEditAcc().getAnchor(0);
			Button add = new Button("Add New Account");
			add.addClickListener(new SwitchPage(anchor));
			addTitle("Login");
			addTitle("First Name");
			addTitle("Last Name");
			addTitle("");
			addTitleWidget(add);
		}

		public void addItem(UserDetailsVO u) {
			String anchor = Store.getFrame().getAdminEditAcc().getAnchor(
					u.getId());
			Hyperlink edit = new Hyperlink("Edit", anchor);
			AddRow r = addRow();
			r.add(u.getLogin());
			r.add(u.getFirstname());
			r.add(u.getLastname());
			r.add(edit);
			r.add(new Button("Delete"));
		}
	}

	AccountTable accounts;

	protected void create() {

		setTitle("Administrate Accounts");
		add(accounts = new AccountTable());

	}

	protected void onShow() {

		OnlinestoreRService.Util.getInstance().getUserDetailList(
				getAsyncHandler());
	}

	protected void onDataArrived(Object o) {

		accounts.cleanExceptHeader();
		UserDetailsVO[] users = (UserDetailsVO[]) o;
		// Window.alert(users.length +"");
		for (int i = 0; i < users.length; i++)
			accounts.addItem(users[i]);

	}

	public String getID() {
		return "AdminAccounts";
	}

}
